using System;
using System.Threading;

using Microsoft.SPOT;

using mfRCF.Core;
using mfRCF.Core.Extensions;

using mfXmlSerializer;

using mfLogger;

using OST.Shared.SharedTypes;
using OST.Persistence;
using OST.Shared;
using OST.Motor;
using OST.Commands.Extensions;

namespace OST.Commands
{
    /// <summary>
    /// drives the motor down
    /// </summary>
    public class MotorMoveDownGETCommand : mfRcfCommand
    {
        #region CTOR

        public MotorMoveDownGETCommand(string httpPath)
            : base(httpPath, new mfGETMethod())
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// wrapper-property 4 logging
        /// </summary>
        private ILoggerAppender Logger
        {
            get
            {
                return mfLogger.mfLogger.Instance;
            }
        }

        #endregion

        #region mfRcfCommand
        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            if (!MotorProvider.Instance.HasInstance)
                this.WriteHttpResponseError(httpContext, new OSTException("no motor-instance available"));

            if (!MotorProvider.Instance.MotorInstance.IsInitialized)
                this.WriteHttpResponseError(httpContext, new OSTException("motor was never initialized"));


            var motor = MotorProvider.Instance.MotorInstance;
            try
            {
                motor.MoveDown();

                var msg = "motor is moving down";
                Logger.Info(msg);
                this.WriteHttpResponseOK(httpContext, msg);
            }
            catch (Exception ex)
            {
                this.WriteHttpResponseError(httpContext, ex);
            }
        }

        #endregion
    }
}
